import { Router } from "express";
import { createProxyMiddleware } from "http-proxy-middleware";
import logger from "../log";
import myConfig from "../config";

const router = Router();

/** 
* @swagger 
* tags:
*   name: Reverse-Proxy-to-RepChain
*   description: 作为指向RepChain节点的反向代理，可通过本服务接口访问被代理的RepChain节点的Rest APIs
* path:
*  /repchain/{RepChain's Rest API}:
*    get:
*      summary: 访问被代理的RepChain节点的Rest API(GET  方法)，具体参数及返回请参考RepChain本身的相应Rest API描述
*      tags: [Reverse-Proxy-to-RepChain]
*    post:
*      summary: 访问被代理的RepChain节点的Rest API(POST 方法)，具体参数及返回请参考RepChain本身的相应Rest API描述
*      tags: [Reverse-Proxy-to-RepChain]
*/
router.use("/", createProxyMiddleware({
    target: myConfig.RepChain.url || "",
    changeOrigin: false,
    ws: true,
    pathRewrite: {
        "^/repchain": "",
    },
    xfwd: true,
    logProvider: () => {
        const myProvider = {
            log: logger.log.bind(logger),
            debug: logger.debug.bind(logger),
            info: logger.info.bind(logger),
            warn: logger.warn.bind(logger),
            error: logger.error.bind(logger),
        };
        return myProvider;
    },
}));

export default router;
